package egean.pad.tool;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;




import android.content.Context;

public class WebService {

	private int index = 0;
	/** webservice命名空间 */
	private static final String NAMESPACE = "http://tempuri.org/";
	/** 登录方法 */
	private static final String METHOD_LOGIN = "LoginCheckFour";
	/** 根据卡号查询信息 */
	private final String METHOD_LoginByCardID = "LoginByCardID";
	/** 激活账户 */
	private final String METHOD_ActivationAccount = "activationAccount_306";
	/** 添加温度 */
	private final String METHOD_AddTemperature = "AddTemperature";
	/** 分页查询温度 */
	private final String METHOD_SelectTHERMOMETERDEVICE_DATA = "SelectTHERMOMETERDEVICE_DATA";
	/** 分页查询血压 */
	private static final String METHOD_SELECTBP = "SelectInfobyBPData";
	/** 查询用户血糖信息 */
	private final String METHOD_SELECTBG = "SelectInfobyBGDEVICE";
	/** 查询用户体重信息 */
	private final String METHOD_SelectInfoBySCALES = "SelectInfoBySCALES";
	/** 添加会员卡返回pn */
	private final String METHOD_custNoImp_307 = "custNoImp_307";

	private Context context;

	public WebService(Context context) {
		this.context = context;
	}

	// #########################################################################################################
	/**
	 * 登录方法
	 * 
	 * @param account
	 *            账户
	 * @param pwd
	 *            密码
	 * @return 操作状态
	 */
	public String Login(String account, String pwd) {
		SoapObject result = null;
		String detailLogin = null;
		try {
			SoapObject rpc = new SoapObject(NAMESPACE, METHOD_LOGIN);
			rpc.addProperty("account", account);
			rpc.addProperty("pwd", pwd);

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.bodyOut = rpc;
			envelope.dotNet = true;
			envelope.setOutputSoapObject(rpc);
			HttpTransportSE ht = new HttpTransportSE(
					ChangeAddress.getAddress(context)[index]);
			ht.debug = true;
			ht.call((NAMESPACE + METHOD_LOGIN), envelope);
			result = (SoapObject) envelope.bodyIn;
			detailLogin = result.getProperty(0).toString();
		} catch (Exception e) {
			e.printStackTrace();
			if (index == 0) {
				index = 1;
				detailLogin = Login(account, pwd);
				index = 0;
				return detailLogin;
			} else {
				index = 0;
			}
		}
		index = 0;
		return detailLogin;
	}

	// #########################################################################################################

	/**
	 * 根据卡号查询信息
	 * @param CardID 卡号
	 * @return
	 */
	public String LoginByCardID(String CardID) {
		SoapObject result = null;
		String detail = null;
		try {
			SoapObject rpc = new SoapObject(NAMESPACE, METHOD_LoginByCardID);
			rpc.addProperty("CardID", CardID);
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.bodyOut = rpc;
			envelope.dotNet = true;
			envelope.setOutputSoapObject(rpc);

			HttpTransportSE ht = new HttpTransportSE(
					ChangeAddress.getAddress(context)[index]);
			ht.debug = true;
			ht.call(NAMESPACE+METHOD_LoginByCardID, envelope);
			result = (SoapObject) envelope.bodyIn;
			detail = result.getProperty(0).toString();
		} catch (Exception e) {
			e.printStackTrace();
			if (index == 0) {
				index = 1;
				detail = LoginByCardID(CardID);
				index = 0;
				return detail;
			} else {
				index = 0;
			}
		}
		index = 0;
		return detail;
	}
	
	

	/**
	 * 激活账户
	 * @param pn 个人编号
	 * @param custAccount  账户
	 * @param areaNo 区域号
	 * @param idNumber 身份证
	 * @param mobile 手机
	 * @param email 邮箱
	 * @param custNo 卡号
	 * @param customerName 姓名
	 * @param sex 些别
	 * @param nationality 民族
	 * @param birthday 生日
	 * @param height 身高
	 * @param weight 体重
	 * @param waistline 腰围
	 * @param bloodType 血型
	 * @param address 地址
	 * @return 操作状态
	 */
	public String activationAccount_306(String pn, String custAccount,
			String areaNo, String idNumber, String mobile, String email,
			String custNo, String customerName, String sex, String nationality,
			String birthday, String height, String weight, String waistline,
			String bloodType, String address) {
		String detail = null;
		SoapObject result = null;

		try {
			SoapObject rpc = new SoapObject(NAMESPACE, METHOD_ActivationAccount);
			rpc.addProperty("pn", pn);
			rpc.addProperty("custAccount", custAccount);
			rpc.addProperty("areaNo", areaNo);
			rpc.addProperty("idNumber", idNumber);
			rpc.addProperty("mobile", mobile);
			rpc.addProperty("email", email);
			rpc.addProperty("custNo", custNo);
			rpc.addProperty("customerName", customerName);

			rpc.addProperty("sex", sex);
			rpc.addProperty("nationality", nationality);
			rpc.addProperty("birthday", birthday);
			rpc.addProperty("height", height);

			rpc.addProperty("weight", weight);
			rpc.addProperty("waistline", waistline);
			rpc.addProperty("bloodType", bloodType);
			rpc.addProperty("address", address);

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.bodyOut = rpc;
			envelope.dotNet = true;
			envelope.setOutputSoapObject(rpc);

			HttpTransportSE ht = new HttpTransportSE(
					ChangeAddress.getAddress(context)[index]);
			ht.debug = true;

			ht.call(NAMESPACE+METHOD_ActivationAccount, envelope);
			result = (SoapObject) envelope.bodyIn;
			detail = result.getProperty(0).toString();
		} catch (Exception e) {
			e.printStackTrace();
			if (index == 0) {
				index = 1;
				detail = activationAccount_306(pn, custAccount, areaNo,
						idNumber, mobile, email, custNo, customerName, sex,
						nationality, birthday, height, weight, waistline,
						bloodType, address);
				index = 0;
				return detail;
			} else {
				index = 0;
			}
		}
		index = 0;
		return detail;
	}
	
	

	/**
	 * 添加新账户，返回账户pn值
	 * @param custNo 芯片id
	 * @param custPwd 密码
	 * @param userId 归属
	 * @param accountCompanySn 归属序列号
	 * @return
	 */
	public String custNoImp_307(String custNo, String custPwd,
			String userId, String accountCompanySn) {
		String detail = null;
		SoapObject result = null;

		try {
			SoapObject rpc = new SoapObject(NAMESPACE, METHOD_custNoImp_307);
			rpc.addProperty("custNo", custNo);
			rpc.addProperty("custPwd", custPwd);
			rpc.addProperty("userId", userId);
			rpc.addProperty("accountCompanySn", accountCompanySn);
			
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.bodyOut = rpc;
			envelope.dotNet = true;
			envelope.setOutputSoapObject(rpc);

			HttpTransportSE ht = new HttpTransportSE(
					ChangeAddress.getAddress(context)[index]);
			ht.debug = true;

			ht.call(NAMESPACE+METHOD_custNoImp_307, envelope);
			result = (SoapObject) envelope.bodyIn;
			detail = result.getProperty(0).toString();
		} catch (Exception e) {
			e.printStackTrace();
			if (index == 0) {
				index = 1;
				detail = custNoImp_307(custNo, custPwd, userId,
						accountCompanySn);
				index = 0;
				return detail;
			} else {
				index = 0;
			}
		}
		index = 0;
		return detail;
	}
	
	// #########################################################################################################

	/**
	 * 查询血糖信息
	 * 
	 * @param pn
	 *            个人编号
	 * @param pagenumber
	 *            数据条数
	 * @param pageindex
	 *            页码
	 * @return 血糖的json数据
	 */

	public String selectgluinformation(String pn, int year, int month) {

		SoapObject result = null;
		String detail = null;
		// 时间切换，判断月份为1月或者12月，改变查询的年份
		int _year, _month;
		try {

			if (month == 12) {
				_year = year + 1;
				_month = 1;
			} else {
				_year = year;
				_month = month + 1;
			}
			SoapObject rpc = new SoapObject(NAMESPACE, METHOD_SELECTBG);
			rpc.addProperty("strWhere", "pn=" + pn
					+ " and GET_BGD_DATE  between to_date('" + year + "-"
					+ month
					+ "-01 00:00:00','yyyy-mm-dd hh24:mi:ss') and to_date('"
					+ _year + "-" + _month
					+ "-01 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			rpc.addProperty("orderby", "order by sn desc");
			rpc.addProperty("pageNumber", 1000);
			rpc.addProperty("pageIndex", 1);
			rpc.addProperty("count", 0);
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.bodyOut = rpc;
			envelope.dotNet = true;
			envelope.setOutputSoapObject(rpc);

			HttpTransportSE ht = new HttpTransportSE(
					ChangeAddress.getAddress(context)[index]);
			ht.debug = true;

			ht.call(NAMESPACE + METHOD_SELECTBG, envelope);
			result = (SoapObject) envelope.bodyIn;
			detail = result.getProperty(0).toString();
		} catch (Exception e) {
			e.printStackTrace();
			if (index == 0) {
				index = 1;
				detail = selectgluinformation(pn, year, month);
				index = 0;
				return detail;
			} else {
				index = 0;
			}
		}
		index = 0;
		return detail;
	}

	/**
	 * 查询用户体重数据
	 * 
	 * @param pn
	 *            账号
	 * @param year
	 *            年
	 * @param month
	 *            月
	 * @return 返回状态
	 */
	public String SelectInfoBySCALES(String pn, int year, int month) {
		SoapObject result = null;
		String detail = null;
		//时间切换，判断月份为1月或者12月，改变查询的年份
		int _year,_month;
		try {
			
			if(month ==12){
				_year = year+1;
				_month = 1;
			}
			else{
				_year = year;
				_month = month+1;
			}
			SoapObject rpc = new SoapObject(NAMESPACE,
					METHOD_SelectInfoBySCALES);
			rpc.addProperty("strWhere", "pn=" + pn
					+ " and GET_SD_DATE  between to_date('" + year + "-"
					+ month
					+ "-01 00:00:00','yyyy-mm-dd hh24:mi:ss') and to_date('"
					+ _year + "-" + _month
					+ "-01 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			rpc.addProperty("orderby", "order by sn desc");
			rpc.addProperty("pageNumber", 1000);
			rpc.addProperty("pageIndex", 1);
			rpc.addProperty("count", 0);
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.bodyOut = rpc;
			envelope.dotNet = true;
			envelope.setOutputSoapObject(rpc);

			HttpTransportSE ht = new HttpTransportSE(ChangeAddress.getAddress(context)[index]);
			ht.debug = true;
			ht.call(NAMESPACE+METHOD_SelectInfoBySCALES, envelope);
			result = (SoapObject) envelope.bodyIn;
			detail = result.getProperty(0).toString();
		} catch (Exception e) {
			e.printStackTrace();
			if (index == 0) {
				index = 1;
				detail = SelectInfoBySCALES(pn, year, month);
				index = 0;
				return detail;
			} else {
				index = 0;
			}
		}
		index = 0;
		return detail;
	}
	
	// #########################################################################################################

	/**
	 * 查询血压信息
	 * 
	 * @param pn
	 *            个人编号
	 * @param pagenumber
	 *            数据条数
	 * @param pageindex
	 *            页码
	 * @return json 查询结果
	 */
	public String selectbpinformation(String pn, int year, int month) {
		SoapObject result = null;
		String detail = null;
		try {
			// 时间切换，判断月份为1月或者12月，改变查询的年份
			int _year, _month;
			if (month == 12) {
				_year = year + 1;
				_month = 1;
			} else {
				_year = year;
				_month = month + 1;
			}
			SoapObject rpc = new SoapObject(NAMESPACE, METHOD_SELECTBP);
			rpc.addProperty("strWhere", "pn=" + pn
					+ " and GET_BPD_DATE  between to_date('" + year + "-"
					+ month
					+ "-01 00:00:00','yyyy-mm-dd hh24:mi:ss') and to_date('"
					+ _year + "-" + _month
					+ "-01 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			rpc.addProperty("orderby", "order by sn desc");
			rpc.addProperty("pageNumber", 1000);
			rpc.addProperty("pageIndex", 1);
			rpc.addProperty("count", 0);
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.bodyOut = rpc;
			envelope.dotNet = true;
			envelope.setOutputSoapObject(rpc);

			HttpTransportSE ht = new HttpTransportSE(
					ChangeAddress.getAddress(context)[index]);
			ht.debug = true;

			ht.call(NAMESPACE + METHOD_SELECTBP, envelope);
			result = (SoapObject) envelope.bodyIn;
			detail = result.getProperty(0).toString();
		} catch (Exception e) {
			e.printStackTrace();
			if (index == 0) {
				index = 1;
				detail = selectbpinformation(pn, year, month);
				index = 0;
				return detail;
			} else {
				index = 0;
			}
		}
		index = 0;
		return detail;
	}
	// #########################################################################################################

	/**
	 * 添加温度
	 * @param temperature 温度
	 * @return
	 */
	public String AddTemperature(String temperature) {
		SoapObject result = null;
		String detail = null;
		try {
			SoapObject rpc = new SoapObject(NAMESPACE, METHOD_AddTemperature);
			rpc.addProperty("strJson", temperature);
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.bodyOut = rpc;
			envelope.dotNet = true;
			envelope.setOutputSoapObject(rpc);

			HttpTransportSE ht = new HttpTransportSE(
					ChangeAddress.getAddress(context)[index]);
			ht.debug = true;
			ht.call(NAMESPACE+METHOD_AddTemperature, envelope);
			result = (SoapObject) envelope.bodyIn;
			detail = result.getProperty(0).toString();
		} catch (Exception e) {
			e.printStackTrace();
			if (index == 0) {
				index = 1;
				detail = AddTemperature(temperature);
				index = 0;
				return detail;
			} else {
				index = 0;
			}
		}
		index = 0;
		return detail;
	}
	// #########################################################################################################

	/**
	 * 查询血压信息
	 * 
	 * @param pn
	 *            个人编号
	 * @param pagenumber
	 *            数据条数
	 * @param pageindex
	 *            页码
	 * @return json 查询结果
	 */
	public String selecttemperatureinformation(String pn, int year, int month) {
		SoapObject result = null;
		String detail = null;
		try {
			// 时间切换，判断月份为1月或者12月，改变查询的年份
			int _year, _month;
			if (month == 12) {
				_year = year + 1;
				_month = 1;
			} else {
				_year = year;
				_month = month + 1;
			}
			SoapObject rpc = new SoapObject(NAMESPACE, METHOD_SelectTHERMOMETERDEVICE_DATA);
			rpc.addProperty("strWhere", "pn=" + pn
					+ " and GET_TD_DATE between to_date('" + year + "-"
					+ month
					+ "-01 00:00:00','yyyy-mm-dd hh24:mi:ss') and to_date('"
					+ _year + "-" + _month
					+ "-01 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			rpc.addProperty("orderby", "order by sn desc");
			rpc.addProperty("pageNumber", 1000);
			rpc.addProperty("pageIndex", 1);
			rpc.addProperty("count", 0);
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.bodyOut = rpc;
			envelope.dotNet = true;
			envelope.setOutputSoapObject(rpc);

			HttpTransportSE ht = new HttpTransportSE(
					ChangeAddress.getAddress(context)[index]);
			ht.debug = true;

			ht.call(NAMESPACE + METHOD_SelectTHERMOMETERDEVICE_DATA, envelope);
			result = (SoapObject) envelope.bodyIn;
			detail = result.getProperty(0).toString();
		} catch (Exception e) {
			e.printStackTrace();
			if (index == 0) {
				index = 1;
				detail = selecttemperatureinformation(pn, year, month);
				index = 0;
				return detail;
			} else {
				index = 0;
			}
		}
		index = 0;
		return detail;
	}
	// #########################################################################################################

	/**
	 * 将毫秒数转换为年月日时分秒的形式
	 * 
	 * @param time
	 *            毫秒时间
	 * @return 转换之后的时间格式
	 */
	public static String millisecondtotime(String time) {
		if (time.length() > 17) {
			time = time.substring(6, time.length() - 2);
		}
		long longtime = Long.parseLong(time);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		Date dt = new Date();
		dt.setTime(longtime);
		String sDateTime = sdf.format(dt); // 格式转换为yyyy-MM-dd HH:mm:ss形式
		return sDateTime;
	}
}
